package com.my.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.my.demo.pojo.FamilyInformation;
import com.my.demo.pojo.Identify;
import com.my.demo.pojo.Person;
import com.my.demo.pojo.Result;
import com.my.demo.service.FamilyInformationService;
import com.my.demo.service.IdentifyService;
import com.my.demo.service.PerIdentifyService;
import com.my.demo.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
public class ReviewController {
  @Autowired
  PersonService personService;
  @Autowired
  IdentifyService identifyService;
  @Autowired
  FamilyInformationService familyInformationService;
  @Autowired
  PerIdentifyService perIdentifyService;

  /*
   * 人物页：插入人物信息，有姓、名、排行、辈分、父亲、性别、排行等
   * 1. 在前端页面点击提交按钮之后，将数据写入到数据库中来,/add_family_person
   * 2. 父亲的id要根据父亲的姓名来得到，
   * 3. 当前复核的家谱的信息：也可以将当前复核的家谱的id放在model中，因此当前复核的家谱的id也从model中获取
   * 此外：还要更新复核表中对应的页面类型字段type值为1，表示人物页
   */
  @PostMapping("/add_family_person")
  @CrossOrigin
  public Result addPerson(@RequestBody Map<String,String> map){
    Result result =new Result();
    result.setSuccess(false);
    result.setDetail(null);

    String lastName= map.get("lastName");
    String firstName=  map.get("firstName");
    String father=  map.get("father");
    String mother =  map.get("mother");
    String childType=  map.get("childType");
    Integer family_archive_id= Integer.parseInt(map.get("family_archive_id"));
    String seniority=  map.get("seniority");
    Integer rank= Integer.parseInt(map.get("rank")) ;
    String biography=  map.get("biography");

    //找到父亲的编号
    QueryWrapper<Person> queryWrapper = new QueryWrapper<Person>();
    queryWrapper.eq("name",father);
    Person father_person = personService.getOne(queryWrapper);
    Integer father_id=null;
    if(father_person!=null){
      father_id = father_person.getPersonId();
    }

    //创建实体，更新数据库
    Person person = new Person(family_archive_id,lastName,firstName,father_id,mother,childType,seniority,biography,rank);
    System.out.println(person);
    personService.saveOrUpdate(person);

    result.setSuccess(true);
    result.setDetail(person);
    result.setMsg("添加人物成功");

    return result;
  }

  @ResponseBody
  @CrossOrigin
  @PostMapping("save_identify_content")
  public Result saveIdentifyContent(@RequestBody Map<String, String> map2)
  {

    Result result = new Result();
    result.setSuccess(false);
    result.setMsg(null);

    String identify_result = map2.get("identify_result");
    Integer family_archive_id = Integer.parseInt(map2.get("family_archive_id"));
    Integer pageNumber = Integer.parseInt(map2.get("pageNumber"));

    UpdateWrapper<Identify> identifyUpdateWrapper = new UpdateWrapper<>();
    identifyUpdateWrapper.eq("family_archive_id", family_archive_id);
    identifyUpdateWrapper.eq("page_number", pageNumber);
    identifyUpdateWrapper.set("result", identify_result);
    identifyService.update(identifyUpdateWrapper);

    result.setMsg("复核内容保存成功");
    result.setDetail(identify_result);

    return result;
  }

  /*
   * 谱序页等信息
   * 从前端传过来家谱的谱序
   * 目前：上传家谱的风格、状态、图片数量等仍然待定
   * 上传家谱的图片数量应该是在家谱上传了进行处理文件之后得到
   * 此外，还要更新对应的复核表identify中对应的字段type值为1，表示谱序页
   */
  @PostMapping("/add_genealogy")
  public Result addGeanealogy(@RequestBody Map<String,String> map) {
    Result result =new Result();
    result.setSuccess(false);
    result.setDetail(null);

    Integer family_archive_id= Integer.parseInt(map.get("family_archive_id"));
    String genealogy=map.get("sequence");//谱序
    String style=map.get("style");
    String changeYear=map.get("changeYear");
    String version=map.get("version");
    String residence=map.get("residence");
    String tName=map.get("tName");
    String other=map.get("other");

    FamilyInformation familyInformation=new FamilyInformation(family_archive_id,genealogy,style,changeYear,version,residence,tName,other);
    familyInformationService.updateById(familyInformation);

    result.setSuccess(true);
    result.setMsg("添加家谱信息成功");
    result.setDetail(familyInformation);

    return result;
  }

  /*
   * 复核之后的内容提交：状态变为已复核
   */
  @ResponseBody
  @PostMapping("submit_identify_content")
  public Result submitIdentifyContent(HttpSession session, @RequestParam("identify_result") String identify_result){
    Result result =new Result();
    result.setSuccess(false);
    result.setDetail(null);

    Integer family_archive_id = (Integer) session.getAttribute("family_archive_id");
    Integer pageNumber = (Integer) session.getAttribute("pageNumber");

    //找到识别表中对应的识别识别页
    QueryWrapper<Identify> queryWrapper = new QueryWrapper<Identify>();
    Map<String,Object> map = new HashMap<>();
    map.put("family_archive_id",family_archive_id);
    map.put("page_number",pageNumber);
    queryWrapper.allEq(map);
    Identify identify = identifyService.getOne(queryWrapper);

    //修改识别内容
    identify.setResult(identify_result);
    identify.setState(1);
    identifyService.saveOrUpdate(identify);

    result.setSuccess(true);
    result.setMsg("提交成功");
    result.setDetail(identify);

    return result;
  }
}
